package com.jin.gulimall.order.listener;

import com.jin.common.to.es.SeckillOrderTo;
import com.jin.gulimall.order.entity.OrderEntity;
import com.jin.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@RabbitListener(queues = "order.seckill.order.queue")
@Service
public class OrderSeckillListener {

    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void handle(SeckillOrderTo seckillOrderTo, Message message, Channel channel) throws IOException {
        System.out.println("秒杀成功，准备创建秒杀单信息...:秒杀单号："+seckillOrderTo.getOrderSn());
        try{
            orderService.createSeckillOrder(seckillOrderTo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            System.out.println("创建秒杀单失败");
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
}
